//
//  InputView.swift
//  onemeetingmac
//
//  Created by 张衡 on 2020/6/11.
//  Copyright © 2020 onemeeting. All rights reserved.
//

import Foundation

class InputView: NSView {
    //0是textFild，1是textView
    var type:Int = 0
    
    
    var confirmClick:ResultString?
    
    override init(frame frameRect: NSRect) {
        super.init(frame: frameRect)
    }
    
    required init?(coder decoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    convenience init(type:Int=0) {
        self.init()
        self.type = type
        self.setupUI()
    }
    
    
    func setupUI() {
        
        self.addSubview(bgTextView)
        bgTextView.snp.makeConstraints { (make) in
            make.width.equalToSuperview()
            make.centerX.equalToSuperview()
            make.top.equalToSuperview()
            if self.type == 0 {
                make.height.equalTo(40)
            }else {
                make.bottom.equalToSuperview().offset(-50)
            }
        }
        
        bgTextView.addSubview(teView)
        teView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview().inset(NSEdgeInsetsMake(8, 5, 8, 5))
        }
        
        bgTextView.addSubview(teFild)
        teFild.snp.makeConstraints { (make) in
            make.height.equalTo(18)
            make.center.equalToSuperview()
            make.width.equalToSuperview().offset(-10)
        }
        
        self.addSubview(canfirmBtn)
        canfirmBtn.snp.makeConstraints { (make) in
            make.width.equalTo(100)
            make.centerX.equalToSuperview()
            make.height.equalTo(36)
            make.bottom.equalTo(0)
        }
        
        self.teView.isHidden = !(self.type==1)
        self.teFild.isHidden = !(self.type==0)
    }
    lazy var bgTextView: NSView = {
        let view = NSView.init(frame: NSRect.zero)
        view.wantsLayer = true
        view.layer?.cornerRadius = 1
        view.layer?.masksToBounds = true
        view.layer?.borderColor = color_gray(30).cgColor
        view.layer?.borderWidth = 1
        view.layer?.backgroundColor = color_dark38.cgColor
        return view
    }()
    lazy var teFild: BaseTextField = {
        let view = BaseTextField.init(frame: NSRect.zero)
        view.maximumNumberOfLines = 1
        view.wantsLayer = true
        view.backgroundColor = color_dark38
        view.font = NSFont.systemFont(ofSize: 14)
        view.textColor = color_gray(230)
        return view
    }()
    
    lazy var teView: BaseTextView = {
        let view = BaseTextView.init(frame: NSRect.zero)
        view.txView.isEditable = true
        view.txView.delegate = self
        view.txView.backgroundColor = color_dark38
        return view
    }()
    
    
    lazy var canfirmBtn: ZHButton = {
        let btn = ZHButton()
        btn.zh_title = "确定".localized()
        btn.zh_font = NSFont.systemFont(ofSize: 16)
        btn.zh_color = NSColor.white
        btn.zh_backgroundColor = color_dark60
        btn.hasBorder = true
        btn.zh_cornerRadius = 18
        btn.action = #selector(canfirmBtnAction)
        btn.target = self
        return btn
    }()
    
    
    @objc func canfirmBtnAction(senber:ZHButton) {
        if self.type == 0 {
            let text = self.teFild.stringValue
            if confirmClick != nil {
                confirmClick!(text)
            }
        }else {
            let text = self.teView.txView.string
            if confirmClick != nil {
                confirmClick!(text)
            }
        }
        
    }
    
}
extension InputView:NSTextFieldDelegate,BaseTextFieldDelegate,NSTextViewDelegate{
    func textFieldDidBecomeFirstResponder(senber: NSTextField) -> Bool {
        //self.teFild.isSelect = true
        return true
    }
    
    func textFieldDidResignFirstResponder(senber: NSTextField) -> Bool {
        //self.teFild.isSelect = false
        return true
    }
    
   
    
    func controlTextDidBeginEditing(_ obj: Notification) {
        ///self.teFild.isSelect = true

    }
    
    func controlTextDidEndEditing(_ obj: Notification) {
        //self.teFild.isSelect = false
    }
    
    
    func controlTextDidChange(_ obj: Notification) {
        guard let texView = obj.object as? BaseTextView else {
            return
        }
        let strInput = texView.txView.string
        let inputLength = strInput.count
        if inputLength > 0 {
            if inputLength > TEXTFIELD_MAX_LENGTH {
                texView.txView.string = texView.txView.string.substring(toIndex: TEXTFIELD_MAX_LENGTH)
                return
            }
        }
        
    }
    
    
}


